﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace dzs.DAL
{
	/// <summary>
	/// 数据访问类:returnGoods
	/// </summary>
	public partial class returnGoods
	{
		public returnGoods()
		{}
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("returnGoodsId", "returnGoods");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int returnGoodsId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from returnGoods");
            strSql.Append(" where returnGoodsId=@returnGoodsId");
            SqlParameter[] parameters = {
					new SqlParameter("@returnGoodsId", SqlDbType.Int,4)
			};
            parameters[0].Value = returnGoodsId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(dzs.Model.returnGoods model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into returnGoods(");
            strSql.Append("return_user_name,return_user_link,user_id,proid,projectName,returnTime,material,mid,inid,return_nums,unit,returnNo)");
            strSql.Append(" values (");
            strSql.Append("@return_user_name,@return_user_link,@user_id,@proid,@projectName,@returnTime,@material,@mid,@inid,@return_nums,@unit,@returnNo)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@return_user_name", SqlDbType.NVarChar,50),
					new SqlParameter("@return_user_link", SqlDbType.VarChar,100),
					new SqlParameter("@user_id", SqlDbType.Int,4),
					new SqlParameter("@proid", SqlDbType.NVarChar,50),
					new SqlParameter("@projectName", SqlDbType.NVarChar,50),
					new SqlParameter("@returnTime", SqlDbType.DateTime),
					new SqlParameter("@material", SqlDbType.NVarChar,250),
					new SqlParameter("@mid", SqlDbType.Int,4),
					new SqlParameter("@inid", SqlDbType.Int,4),
					new SqlParameter("@return_nums", SqlDbType.Decimal,9),
					new SqlParameter("@unit", SqlDbType.NChar,10),
					new SqlParameter("@returnNo", SqlDbType.VarChar,50)};
            parameters[0].Value = model.return_user_name;
            parameters[1].Value = model.return_user_link;
            parameters[2].Value = model.user_id;
            parameters[3].Value = model.proid;
            parameters[4].Value = model.projectName;
            parameters[5].Value = model.returnTime;
            parameters[6].Value = model.material;
            parameters[7].Value = model.mid;
            parameters[8].Value = model.inid;
            parameters[9].Value = model.return_nums;
            parameters[10].Value = model.unit;
            parameters[11].Value = model.returnNo;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(dzs.Model.returnGoods model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update returnGoods set ");
            strSql.Append("return_user_name=@return_user_name,");
            strSql.Append("return_user_link=@return_user_link,");
            strSql.Append("user_id=@user_id,");
            strSql.Append("proid=@proid,");
            strSql.Append("projectName=@projectName,");
            strSql.Append("returnTime=@returnTime,");
            strSql.Append("material=@material,");
            strSql.Append("mid=@mid,");
            strSql.Append("inid=@inid,");
            strSql.Append("return_nums=@return_nums,");
            strSql.Append("unit=@unit,");
            strSql.Append("returnNo=@returnNo");
            strSql.Append(" where returnGoodsId=@returnGoodsId");
            SqlParameter[] parameters = {
					new SqlParameter("@return_user_name", SqlDbType.NVarChar,50),
					new SqlParameter("@return_user_link", SqlDbType.VarChar,100),
					new SqlParameter("@user_id", SqlDbType.Int,4),
					new SqlParameter("@proid", SqlDbType.NVarChar,50),
					new SqlParameter("@projectName", SqlDbType.NVarChar,50),
					new SqlParameter("@returnTime", SqlDbType.DateTime),
					new SqlParameter("@material", SqlDbType.NVarChar,250),
					new SqlParameter("@mid", SqlDbType.Int,4),
					new SqlParameter("@inid", SqlDbType.Int,4),
					new SqlParameter("@return_nums", SqlDbType.Decimal,9),
					new SqlParameter("@unit", SqlDbType.NChar,10),
					new SqlParameter("@returnNo", SqlDbType.VarChar,50),
					new SqlParameter("@returnGoodsId", SqlDbType.Int,4)};
            parameters[0].Value = model.return_user_name;
            parameters[1].Value = model.return_user_link;
            parameters[2].Value = model.user_id;
            parameters[3].Value = model.proid;
            parameters[4].Value = model.projectName;
            parameters[5].Value = model.returnTime;
            parameters[6].Value = model.material;
            parameters[7].Value = model.mid;
            parameters[8].Value = model.inid;
            parameters[9].Value = model.return_nums;
            parameters[10].Value = model.unit;
            parameters[11].Value = model.returnNo;
            parameters[12].Value = model.returnGoodsId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int returnGoodsId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from returnGoods ");
            strSql.Append(" where returnGoodsId=@returnGoodsId");
            SqlParameter[] parameters = {
					new SqlParameter("@returnGoodsId", SqlDbType.Int,4)
			};
            parameters[0].Value = returnGoodsId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string returnGoodsIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from returnGoods ");
            strSql.Append(" where returnGoodsId in (" + returnGoodsIdlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public dzs.Model.returnGoods GetModel(int returnGoodsId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 returnGoodsId,return_user_name,return_user_link,user_id,proid,projectName,returnTime,material,mid,inid,return_nums,unit,returnNo from returnGoods ");
            strSql.Append(" where returnGoodsId=@returnGoodsId");
            SqlParameter[] parameters = {
					new SqlParameter("@returnGoodsId", SqlDbType.Int,4)
			};
            parameters[0].Value = returnGoodsId;

            dzs.Model.returnGoods model = new dzs.Model.returnGoods();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["returnGoodsId"] != null && ds.Tables[0].Rows[0]["returnGoodsId"].ToString() != "")
                {
                    model.returnGoodsId = int.Parse(ds.Tables[0].Rows[0]["returnGoodsId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["return_user_name"] != null && ds.Tables[0].Rows[0]["return_user_name"].ToString() != "")
                {
                    model.return_user_name = ds.Tables[0].Rows[0]["return_user_name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["return_user_link"] != null && ds.Tables[0].Rows[0]["return_user_link"].ToString() != "")
                {
                    model.return_user_link = ds.Tables[0].Rows[0]["return_user_link"].ToString();
                }
                if (ds.Tables[0].Rows[0]["user_id"] != null && ds.Tables[0].Rows[0]["user_id"].ToString() != "")
                {
                    model.user_id = int.Parse(ds.Tables[0].Rows[0]["user_id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["proid"] != null && ds.Tables[0].Rows[0]["proid"].ToString() != "")
                {
                    model.proid = ds.Tables[0].Rows[0]["proid"].ToString();
                }
                if (ds.Tables[0].Rows[0]["projectName"] != null && ds.Tables[0].Rows[0]["projectName"].ToString() != "")
                {
                    model.projectName = ds.Tables[0].Rows[0]["projectName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["returnTime"] != null && ds.Tables[0].Rows[0]["returnTime"].ToString() != "")
                {
                    model.returnTime = DateTime.Parse(ds.Tables[0].Rows[0]["returnTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["material"] != null && ds.Tables[0].Rows[0]["material"].ToString() != "")
                {
                    model.material = ds.Tables[0].Rows[0]["material"].ToString();
                }
                if (ds.Tables[0].Rows[0]["mid"] != null && ds.Tables[0].Rows[0]["mid"].ToString() != "")
                {
                    model.mid = int.Parse(ds.Tables[0].Rows[0]["mid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["inid"] != null && ds.Tables[0].Rows[0]["inid"].ToString() != "")
                {
                    model.inid = int.Parse(ds.Tables[0].Rows[0]["inid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["return_nums"] != null && ds.Tables[0].Rows[0]["return_nums"].ToString() != "")
                {
                    model.return_nums = decimal.Parse(ds.Tables[0].Rows[0]["return_nums"].ToString());
                }
                if (ds.Tables[0].Rows[0]["unit"] != null && ds.Tables[0].Rows[0]["unit"].ToString() != "")
                {
                    model.unit = ds.Tables[0].Rows[0]["unit"].ToString();
                }
                if (ds.Tables[0].Rows[0]["returnNo"] != null && ds.Tables[0].Rows[0]["returnNo"].ToString() != "")
                {
                    model.returnNo = ds.Tables[0].Rows[0]["returnNo"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select returnGoodsId,return_user_name,return_user_link,user_id,proid,projectName,returnTime,material,mid,inid,return_nums,unit,returnNo ");
            strSql.Append(" FROM returnGoods ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" returnGoodsId,return_user_name,return_user_link,user_id,proid,projectName,returnTime,material,mid,inid,return_nums,unit,returnNo ");
            strSql.Append(" FROM returnGoods ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM returnGoods ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.returnGoodsId desc");
            }
            strSql.Append(")AS Row, T.*  from returnGoods T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "returnGoods";
            parameters[1].Value = "returnGoodsId";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
	}
}

